import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    search: "",
    startDate: '',
    endDate: '',
    shade: false,
    materialDetail: false,
    isdaterange: false,
    list: [],
  },
  onLoad() {
    this.setData({
      startDate: dayjs().startOf('month').format('YYYY/MM/DD'),
      endDate: dayjs().endOf('month').format('YYYY/MM/DD')
    })
  },
  onShow() {
    this.getQualityControlQuery();
  },
  add() {
    dd.navigateTo({
      url: '/pages/qualityInfoAddEdit/qualityInfoAddEdit?type=add'
    })
  },

  // 查看质检结果
  getQualityControlQuery() {
    let that = this
    $http(apiSetting.qualityControlQuery, {
      search: that.data.search,
      startTime: dayjs(that.data.startDate).startOf('day').format('YYYY-MM-DD HH:mm:ss'),
      endTime: dayjs(that.data.endDate).endOf('day').format('YYYY-MM-DD HH:mm:ss'),
      pageId: 1, //页码
      pageSize: 200, //每页数
    }).then((data) => {
      that.setData({
        list: data.result.record
      })
    })
  },

  toQualityResult(e) {
    let id = e.target.dataset.item.id
    dd.navigateTo({
      url: '/pages/qualityResult/qualityResult?id=' + id
    })
  },
  showMatDet() {
    this.setData({
      shade: true,
      materialDetail: true
    })
  },
  hiddenMatDet() {
    this.setData({
      shade: false,
      materialDetail: false
    })
  },
  // 选择时间
  selectDate() {
    this.setData({
      isdaterange: true
    })
  },
  onAffirm(data) {
    console.log(data)
    if (data.result) {
      this.setData({
        startDate: dayjs(data.startDate).format('YYYY/MM/DD'),
        endDate: dayjs(data.endDate).format('YYYY/MM/DD')
      })
      this.getQualityControlQuery()
    }
    this.setData({
      isdaterange: false
    })
  },
  // 展开
  onUnfold(e) {
    let index = e.target.dataset.index
    this.data.list[index].isUnfold = !this.data.list[index].isUnfold
    this.setData({
      list: this.data.list
    })
  },
  onScan() {
    let that = this
    dd.scan({
      type: 'qr',
      success: (res) => {
        that.setData({
          search: res.code
        })
        that.getQualityControlQuery()
      },
    });
  },
  onInputSearch(e) {
    let that = this
    clearTimeout(that.data.timeOut)
    that.data.search = e.detail.value
    that.data.timeOut = setTimeout(function () {
      that.getQualityControlQuery()
    }, 1000);
  }
});
